package com.example.zyfypt_611cgt.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.zyfypt_611cgt.R;
import com.example.zyfypt_611cgt.bean.LoginBean;
import com.example.zyfypt_611cgt.iface.LoginListener;
import com.example.zyfypt_611cgt.model.LoginModel;

public class LoginActivity extends AppCompatActivity {
    private EditText etuser, etpass;
    private Button btnlogin, btnregister;
    private Switch sw;//1111
    private String username="", password="", sessionID="";
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID = loginBean.getSessionid();
            Log.i("LoginActivity","----sessionID=" + sessionID);
            if(sessionID != null) {
                //判开关，存密码
                if(sw.isChecked()){
                    saveSP(etuser.getText().toString(),
                            etpass.getText().toString(),
                            sw.isChecked(),
                            sessionID);
                }
                Toast.makeText(LoginActivity.this, "登录成功" + sessionID, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                saveSP("", "", false, "");
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
        }
        public void onFail(String msg) {
            saveSP("", "", false, "");
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

      /*private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name", etuser.getText().toString());
        editor.putString("pass", etpass.getText().toString());
        editor.putBoolean("state", sw.isChecked());
        editor.putString("session", sessionID);
        editor.commit();
    }*/

    private void saveSP(String name, String pass, Boolean state, String session) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name", name);
        editor.putString("pass", pass);
        editor.putBoolean("state", state);
        editor.putString("session", session);
        editor.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initViews();

        sp = getSharedPreferences("login", MODE_PRIVATE);

        readSP();
    }

    private void readSP() {
        String name = sp.getString("name", "");
        String pass = sp.getString("pass", "");
        Boolean state = sp.getBoolean("state", false);
        if(state){
            etuser.setText(name);
            etpass.setText(pass);
        }
        sw.setChecked(state);
    }

    private void initViews() {
        etuser = findViewById(R.id.etuser);
        etpass = findViewById(R.id.etpass);
        btnlogin = findViewById(R.id.button);
        btnregister = findViewById(R.id.button1);
        sw = findViewById(R.id.switch1);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = etuser.getText().toString();
                password = etpass.getText().toString();
                LoginModel loginModel = new LoginModel();
                loginModel.getLoginResult(username, password, loginListener);
            }
        });

        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}